<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 邀请用户 开发者: 麦沃德科技-半夏  
+---------------------------------------------------------------------- -->

<template>
	<view class="container" v-if="loadEnd">
		<!-- 标题栏 -->
		<title-bar title="邀请用户" :showBack="true" :backgroundColor="titleBackground" :frontColor="titleColor"></title-bar>
		<!-- 顶部内容 -->
		<view class="container-header">
			<view class="header-btn flex align-items-center" @click="toAgreement()">
				<text class="btn-text">活动规则</text>
				<image class="btn-icon" src="/static/passenger/right_w.png" mode="aspectFit"></image>
			</view>
		</view>
		<!-- 页面内容 -->
		<view class="container-main">
			<view class="main-title">邀请<text class="text">新用户</text>下单 赚现金</view>
			<swiper class="main-notice" disable-touch vertical circular :interval="2200" autoplay v-if="invitationInfo.make_money.length">
				<swiper-item class="notice-item" v-for="(item, index) in invitationInfo.make_money" :key="index">
					<view class="item-box flex align-items-center">
						<image class="box-avatar" :src="item.avatar" mode="aspectFill"></image>
						<view class="box-name">{{item.mobile}}</view>
						<view class="box-tips">已赚<text class="text">{{item.amount}}</text>元</view>
					</view>
				</swiper-item>
			</swiper>
			<view class="main-packet">
				<image class="packet-bg" src="/static/passenger/packet.png" mode="widthFix"></image>
				<view class="packet-content">
					<view class="content-title">邀请1位新用户</view>
					<view class="content-title">完成第1笔订单你赚</view>
					<view class="content-amount"><text class="text">{{invitationInfo.first_cashback_gift_amount}}</text>元</view>
				</view>
			</view>
			<button class="main-btn flex flex-center clear" open-type="share" v-if="token">
				<image class="btn-icon" src="/static/passenger/WeChat.png" mode="aspectFit"></image>
				<text class="btn-text">立即赚钱</text>
			</button>
			<view class="main-btn flex flex-center" @click="toLogin()" v-else>
				<image class="btn-icon" src="/static/passenger/WeChat.png" mode="aspectFit"></image>
				<text class="btn-text">立即赚钱</text>
			</view>
			<view class="main-card" v-if="token">
				<view class="card-head flex flex-between-center">
					<view class="head-title">已累计收入<text class="text">{{invitationInfo.accumulated_income}}</text>元</view>
					<view class="head-btn flex align-items-center" @click="toWithdrawal()">
						<text class="btn-text">去提现</text>
						<image class="btn-iocn" src="/static/passenger/right_r.png" mode="aspectFit"></image>
					</view>
				</view>
				<view class="card-record">
					<view class="record-classify">
						<view class="classify-item" :class="{active: selectClassify == 2}" @click="changeClassify(2)">待完成</view>
						<view class="classify-item" :class="{active: selectClassify == 1}" @click="changeClassify(1)">已完成</view>
					</view>
					<view class="record-list" v-if="incomeList.length">
						<view class="list-item flex align-items-center" v-for="(item, index) in incomeList" :key="index">
							<image class="item-avatar" :src="item.avatar" mode="aspectFill"></image>
							<view class="item-name text-ellipsis flex-item">{{item.mobile}}</view>
							<view class="item-time">{{item.time}}</view>
						</view>
					</view>
					<view class="record-empty" v-else>
						<image class="empty-icon" src="/static/passenger/invitation_empty.png" mode="aspectFit"></image>
						<view class="empty-text">邀请好友下单，你可赚现金</view>
						<button class="empty-btn flex align-items-center clear" open-type="share">
							<text class="btn-text">邀请好友下单</text>
							<image class="btn-icon" src="/static/passenger/right_r.png" mode="aspectFit"></image>
						</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import titleBar from "@/pages/component/title-bar/title-bar.vue"
	export default {
		components: {
			titleBar,
		},
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 标题栏背景颜色
				titleBackground: "transparent",
				// 标题栏字体颜色
				titleColor: "white",
				// 邀请信息
				invitationInfo: "",
				// 已选分类
				selectClassify: 2,
				// 收益列表
				incomeList: [],
				// 分类查询参数
				page: 1,
				limit: 10,
				hasMore: false,
			}
		},
		computed: {
			...mapState({
				shareTitle: state => state.app.shareTitle,
				shareImage: state => state.app.shareImage,
				token: state => {
					if (state.user.token) uni.showShareMenu()
					else uni.hideShareMenu()
					return state.user.token
				},
			}),
		},
		onLoad() {
			uni.setNavigationBarColor({
				frontColor: "#ffffff",
				backgroundColor: "transparent"
			})
			uni.showLoading({
				title: "加载中"
			})
			this.getIncomeList()
			this.getInvitationInfo(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onPageScroll(e) {
			const scrollTop = e.scrollTop
			if (scrollTop > 100) {
				this.titleBackground = "#fff"
				this.titleColor = "black"
			} else {
				let opacity = parseFloat(scrollTop / 100).toFixed(4)
				this.titleBackground = "rgba(255, 255, 255, " + opacity + ")"
				if (scrollTop > 30) {
					this.titleColor = "black"
					uni.setNavigationBarColor({
						frontColor: "#000000", //文字颜色
						backgroundColor: "#ffffff" //底部背景色
					})
				} else {
					this.titleColor = "white"
					uni.setNavigationBarColor({
						frontColor: "#ffffff", //文字颜色
						backgroundColor: "transparent" //底部背景色
					})
				}
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.getIncomeList()
			this.getInvitationInfo(() => {
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getIncomeList()
			}
		},
		onShareAppMessage() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
				path: "/pages/passenger/index/index?wechat_id=" + this.invitationInfo.wechat_id,
			}
		},
		onShareTimeline() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
				path: "/pages/passenger/index/index?wechat_id=" + this.invitationInfo.wechat_id,
			}
		},
		methods: {
			// 获取邀请信息
			getInvitationInfo(fn) {
				this.$util.request("passenger.invitation.information").then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.invitationInfo = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取邀请信息 ', error)
				})
			},
			// 获取收益列表
			getIncomeList() {
				if (!uni.getStorageSync("token")) {
					this.incomeList = []
					return
				}
				this.$util.request("passenger.invitation.incomeList", {
					page: this.page,
					limit: this.limit,
					state: this.selectClassify,
				}).then(res => {
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.total / this.limit ? true : false
						this.incomeList = this.page == 1 ? list : [...this.incomeList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取收益列表 ', error)
				})
			},
			// 切换分类
			changeClassify(id) {
				this.selectClassify = id
				this.getIncomeList()
			},
			// 跳转活动规则
			toAgreement() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/invitation/agreement"
				})
			},
			// 去提现
			toWithdrawal() {
				if (!this.$util.verifyLogin()) return
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/wallet/index",
					isToken: true,
				})
			},
			// 跳转登录
			toLogin() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/public/login/index"
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 0;
	}

	.container {
		background: linear-gradient(180deg, #F82634 0%, #FEB268 100%);
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		min-height: 100vh;

		.container-header {
			display: flex;
			justify-content: flex-end;
			padding-top: 16rpx;

			.header-btn {
				border-radius: 24rpx 0 0 24rpx;
				background: rgba(0, 0, 0, 0.20);
				padding: 0 16rpx;

				.btn-text {
					color: #FFF;
					text-align: center;
					font-size: 24rpx;
					line-height: 48rpx;
				}

				.btn-icon {
					width: 24rpx;
					height: 24rpx;
					margin-left: 8rpx;
				}
			}
		}

		.container-main {
			padding: 48rpx 52rpx;

			.main-title {
				color: #FFF;
				text-align: center;
				text-shadow: 0 0 20rpx rgba(78, 0, 7, 0.25);
				font-size: 48rpx;
				line-height: 60rpx;

				.text {
					color: #FFEB79;
				}
			}

			.main-notice {
				position: relative;
				z-index: 8;
				margin-top: 32rpx;
				height: 72rpx;

				.notice-item {
					display: flex;
					justify-content: center;
					padding: 4rpx 0;
					box-sizing: border-box;

					.item-box {
						height: 100%;
						padding: 0 32rpx 0 8rpx;
						border-radius: 32rpx;
						background: #F20004;

						.box-avatar {
							width: 48rpx;
							height: 48rpx;
							border-radius: 50%;
						}

						.box-name {
							margin-left: 8rpx;
							color: #FFF;
							font-size: 28rpx;
							line-height: 40rpx;
						}

						.box-tips {
							margin-left: 32rpx;
							color: #FFF;
							font-size: 28rpx;
							line-height: 40rpx;

							.text {
								color: #FFEB79;
							}
						}
					}
				}
			}

			.main-packet {
				margin-top: -36rpx;
				position: relative;

				.packet-bg {
					width: 100%;
					height: auto;
				}

				.packet-content {
					position: absolute;
					top: 17%;
					left: 17%;
					right: 17%;
					bottom: 10%;

					.content-title {
						color: #F82634;
						text-align: center;
						font-size: 28rpx;
						line-height: 40rpx;
					}

					.content-amount {
						text-align: center;
						font-size: 28rpx;
						font-weight: 600;
						line-height: 230rpx;
						color: #F82634;

						.text {
							font-size: 192rpx;
							font-weight: 700;
							background: linear-gradient(180deg, #F87254 0%, #F82634 100%);
							background-clip: text;
							-webkit-background-clip: text;
							-webkit-text-fill-color: transparent;
						}
					}
				}
			}

			.main-btn {
				margin: 20rpx 12rpx 0;
				border-radius: 64rpx;
				border: 1px solid rgba(255, 255, 255, 0.50);
				background: linear-gradient(180deg, #FEB268 0%, #F82634 100%);
				padding: 34rpx 32rpx;

				.btn-icon {
					width: 56rpx;
					height: 56rpx;
				}

				.btn-text {
					margin-left: 16rpx;
					color: #FFF;
					text-align: center;
					font-size: 40rpx;
					line-height: 40rpx;
				}
			}

			.main-card {
				margin-top: 48rpx;
				border-radius: 32rpx;
				background: #FFF;

				.card-head {
					border-radius: 32rpx 32rpx 0 0;
					background: #FFF2F2;
					padding: 32rpx;

					.head-title {
						color: #242629;
						font-size: 32rpx;
						font-weight: 600;
						line-height: 44rpx;

						.text {
							color: #FA2C3E;
						}
					}

					.head-btn {
						border-radius: 24rpx;
						border: 2rpx solid #FA2C3E;
						padding: 4rpx 14rpx;

						.btn-text {
							color: #FA2C3E;
							font-size: 24rpx;
							font-weight: 600;
							line-height: 36rpx;
						}

						.btn-iocn {
							width: 24rpx;
							height: 24rpx;
							margin-left: 8rpx;
						}
					}
				}

				.card-record {
					padding: 32rpx 8rpx;

					.record-classify {
						display: flex;
						align-items: flex-end;

						.classify-item {
							padding: 0 24rpx;
							color: #242629;
							text-align: center;
							font-size: 24rpx;
							font-weight: 600;
							line-height: 34rpx;

							&.active {
								color: #FA2C3E;
								font-size: 28rpx;
								font-weight: 600;
								line-height: 40rpx;
							}
						}
					}

					.record-list {
						padding: 0 24rpx;
						margin-top: 32rpx;
						min-height: 420rpx;

						.list-item {
							margin-top: 32rpx;
							padding: 8rpx 12rpx 8rpx 8rpx;
							border-radius: 32rpx;
							background: #F5F5F5;

							.item-avatar {
								width: 36rpx;
								height: 36rpx;
								border-radius: 50%;
							}

							.item-name {
								margin-left: 12rpx;
								color: #242629;
								font-size: 24rpx;
								line-height: 36rpx;
							}

							.item-time {
								margin-left: 12rpx;
								color: #8D929C;
								font-size: 24rpx;
								line-height: 34rpx;
							}
						}
					}

					.record-empty {
						padding: 0 24rpx;
						min-height: 452rpx;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						.empty-icon {
							width: 160rpx;
							height: 140rpx;
						}

						.empty-text {
							margin-top: 24rpx;
							color: #8D929C;
							text-align: center;
							font-size: 24rpx;
							font-weight: 600;
							line-height: 32rpx;
						}

						.empty-btn {
							margin-top: 16rpx;
							border-radius: 40rpx;
							border: 2rpx solid #FA2C3E;
							padding: 16rpx 32rpx;

							.btn-text {
								color: #FA2C3E;
								font-size: 24rpx;
								font-weight: 600;
								line-height: 32rpx;
							}

							.btn-icon {
								width: 24rpx;
								height: 24rpx;
								margin-left: 8rpx;
							}
						}
					}
				}
			}
		}
	}
</style>